package com.baijiaoxi.common.model.base;


import com.baijiaoxi.common.constant.HttpCode;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

/**
 * Created by shixj on 2017/7/21.
 */
@Slf4j
@Data
@ApiModel(value = "统一返回对象")
public class CommonResponse<T> {
    @ApiModelProperty("请求ID")
    private String requestId;//请求ID
    @ApiModelProperty("状态码")
    private String status;//状态码
    @ApiModelProperty("状态信息")
    private String msg;//状态信息
    @ApiModelProperty("数据对象")
    private T data;//数据对象
    @ApiModelProperty("错误码")
    private Integer errCode = 0;


    public void init() {
        try {
            this.requestId = MDC.get("requestId");
        } catch (Exception e) {

        }
    }

    public CommonResponse() {
        init();
    }

    public CommonResponse(String statusCode, String msg, T data) {
        init();
        this.status = statusCode;
        this.msg = msg;
        this.data = data;
    }

    public CommonResponse(String statusCode, int errCode, String msg, T data) {
        init();
        this.errCode = errCode;
        this.status = statusCode;
        this.msg = msg;
        this.data = data;
    }

    public CommonResponse(String statusCode, int errCode, String msg) {
        init();
        this.errCode = errCode;
        this.status = statusCode;
        this.msg = msg;
    }


    public CommonResponse(String statusCode, T data) {
        init();
        this.status = statusCode;
        this.data = data;
        this.msg = "";
    }


    /**
     * default
     * success
     * 200
     *
     * @param <E>
     * @return
     */

    public static <E> CommonResponse of() {
        return of("请求成功！", null);
    }

    public static <E> CommonResponse of(E data) {
        return of("请求成功！", data);
    }

    public static <E> CommonResponse of(String msg, E data) {
        return new CommonResponse(HttpCode.SUCCESS_GET, msg, data);
    }

    /**
     * add
     * success
     * 201
     *
     * @param <E>
     * @return
     */
    public static <E> CommonResponse ofAddSuccess() {
        return ofAddSuccess("保存成功！", null);
    }

    public static <E> CommonResponse ofAddSuccess(E data) {
        return ofAddSuccess("保存成功！", data);
    }

    public static <E> CommonResponse ofAddSuccess(String msg, E data) {
        return new CommonResponse(HttpCode.SUCCESS_ADD, msg, data);
    }

    /**
     * get
     * success
     * 200
     *
     * @param <E>
     * @return
     */

    public static <E> CommonResponse ofGetSuccess() {
        return ofGetSuccess("查询成功！", null);
    }

    public static <E> CommonResponse ofGetSuccess(E data) {
        return ofGetSuccess("查询成功！", data);
    }

    public static <E> CommonResponse ofGetSuccess(String msg, E data) {
        return new CommonResponse(HttpCode.SUCCESS_GET, msg, data);
    }

    /**
     * update
     * success
     * 202
     *
     * @param <E>
     * @return
     */

    public static <E> CommonResponse ofUpdateSuccess() {
        return ofUpdateSuccess("保存成功！", null);
    }

    public static <E> CommonResponse ofUpdateSuccess(E data) {
        return ofUpdateSuccess("保存成功！", data);
    }

    public static <E> CommonResponse ofUpdateSuccess(String msg, E data) {
        return new CommonResponse(HttpCode.SUCCESS_UPDATE, msg, data);
    }

    /**
     * del
     * success
     * 204
     *
     * @param <E>
     * @return
     */
    public static <E> CommonResponse ofDelSuccess() {
        return ofDelSuccess("删除成功！", null);
    }

    public static <E> CommonResponse ofDelSuccess(E data) {
        return ofDelSuccess("删除成功！", data);
    }

    public static <E> CommonResponse ofDelSuccess(String msg, E data) {
        return new CommonResponse(HttpCode.SUCCESS_DEL, msg, data);
    }

    /**
     * bad request
     * 204
     *
     * @param <E>
     * @return
     */
    public static <E> CommonResponse ofFail() {
        return ofFail("请求失败", null);
    }

    public static <E> CommonResponse ofFail(E data) {
        return ofFail("请求失败", data);
    }

    public static <E> CommonResponse ofFail(String msg, E data) {
        return new CommonResponse(HttpCode.ERROR_BAD_REQUEST, msg, data);
    }

    public static <E> CommonResponse ofError(Error error, Object... args) {
        return ofError(null, error, args);
    }

    public static <E> CommonResponse ofError(E data, Error error, Object... args) {
        return ofException(new CoreException(data, error, args));
    }

    /**
     * CoreException
     *
     * @param e
     * @param <E>
     * @return
     */
    public static <E> CommonResponse ofException(CoreException e) {
        Error error = e.getError();
        return new CommonResponse(HttpCode.ERROR_BAD_REQUEST, error == null ? -1 : e.getError().value(), e.getMessage(), e.getData());
    }

    /**
     * Exception
     *
     * @param e
     * @param <E>
     * @return
     */
    public static <E> CommonResponse ofException(Exception e) {
        return new CommonResponse(HttpCode.ERROR_BAD_REQUEST, e.getMessage(), null);
    }

    @JsonIgnore
    public boolean isGetSuccess() {
        return HttpCode.isGetSuccess(this.status);
    }

    @JsonIgnore
    public boolean isAddSuccess() {
        return HttpCode.isAddSuccess(this.status);
    }

    @JsonIgnore
    public boolean isUpdateSuccess() {
        return HttpCode.isUpdateSuccess(this.status);
    }

    @JsonIgnore
    public boolean isDelSuccess() {
        return HttpCode.isDelSuccess(this.status);
    }

    @JsonIgnore
    public boolean isSuccess() {
        return HttpCode.isSuccess(this.status);
    }

    @JsonIgnore
    public boolean isError() {
        return HttpCode.isError(this.status);
    }

    public T successData() {
        if (isError()) {
            log.error("异常返回结果：", this);
            throw CoreException.create(CommonError.httpRequestFail, this.msg);
        }


        return data;
    }
}
